package demo.isolib.movement {
	import as3isolib.geom.Pt;
	
	import com.greensock.TimelineLite;
	import com.greensock.TweenLite;
	import com.greensock.easing.Linear;
	
	import demo.isolib.Tile;
	import demo.isolib.TileEvent;
	import com.sshc625.log.Logger;
	
	import masputih.pathfinding.astar.AStar;
	import masputih.pathfinding.astar.AStarEvent;
	import masputih.pathfinding.astar.AStarGrid;
	import masputih.pathfinding.astar.GridView;

	/**
	 */
	public class AIMove extends TileMovement {
		public static const TWEEN_DURATION:Number = .24;

		protected var _astarGrid:AStarGrid;
		protected var _astar:AStar;
		protected var _animator:TimelineLite;

		public function AIMove(spd:uint=100) {
			super(spd);
		}

		override public function execute(params:Array = null):void {
			Logger.log("AIMove.execute(" + params + ")");
			super.execute(params);
			updateAstarGrid(true);

			_astarGrid = tile.world.astarGrid.clone();
			_astarGrid.setStartNode(tile.column, tile.row);
			_astarGrid.setEndNode(params[0], params[1]);
			GridView.getInstance().setGrid(_astarGrid);
			GridView.getInstance().drawGrid();

			var nonwalkables:Array = _astarGrid.getNonWalkables();

			for (var i:int = 0; i < nonwalkables.length; i++) {
				var cr:Array = String(nonwalkables[i]).split('_');
				var c:Number = cr[0];
				var r:Number = cr[1];
				_astarGrid.setWalkable(c, r, false);

			}

			_astar = new AStar(_astarGrid);
			_astar.addEventListener(AStarEvent.COMPLETE, onPathComplete);
			_astar.addEventListener(AStarEvent.NO_PATH, onNoPath);
			_astar.findPath();

		}

		private function onNoPath(e:AStarEvent):void {
			tile.dispatchEvent(new TileEvent(TileEvent.CANT_MOVE));
			updateAstarGrid(tile.walkable);

			removeAStar();
		}

		protected function onPathComplete(e:AStarEvent):void {
			removeAStar();

			var path:Array = e.path;
			GridView.getInstance().showPath(path);
			var csize:Number = tile.grid.cellSize;

			_animator = new TimelineLite({"onStart": onMoveStart, "onComplete": onMoveComplete});
			_animator.autoRemoveChildren = true;

			for (var i:int = 0, len:Number = path.length; i < len; i++) {
				var c:Number = path[i].column;
				var r:Number = path[i].row
				var x:Number = c * csize;
				var y:Number = r * csize;
				
				var t:TweenLite = new TweenLite(tile, TWEEN_DURATION, {"x": x, "y": y, "onStart": onTweenStart, "onStartParams": [x, y], "ease": Linear.easeNone});

				_animator.append(t);
			}

			_animator.play();
		}

		private function removeAStar():void {
			_astar.removeEventListener(AStarEvent.COMPLETE, onPathComplete);
			_astar.removeEventListener(AStarEvent.NO_PATH, onNoPath);
			_astar = null;
		}

		protected function onMoveStart():void {
			tile.isIdle = false;
		}

		protected function onTweenStart(... params):void {
			var x:Number = params[0];
			var y:Number = params[1];
			tile.faceDirection = getFaceDirection(x, y);
		}

		protected function onMoveComplete():void {
			updateAstarGrid(tile.walkable);
			tile.isIdle = true;
			complete();
		}

	}

}